<template>
<div>
   
    <Button type="primary" @click="addSchool">添加学校</Button>
    
    <Table border :columns="columns" :data="data"></Table>
    <Page :total="paging.total" @on-change="changepage" :page-size="paging.size"></Page>
</div>
</template>
<script>
import util from '../libs/util';
    export default {

        created() {
            this.initFormatter(); 
            this.paging = {
                page: 1,
                size: 8,
                status: 1,
            };
            this.load_data(this.paging);
            this.columns = [
                    {
                        title: '学校ID',
                        key: 'id',
                    },
                    {
                        title: '学校名称',
                        key: 'name',
                    },
                    {
                        title: '添加时间',
                        key: 'time',
                        render: function (h, params) {
                            return h('div', 
                               new Date(this.row.time*1000).Format('yyyy-MM-dd mm:ss')
                            );
                        }
                    },
                    {
                        title: '操作',
                        key: 'action',
                        width: 150,
                        align: 'center',
                        render: (h, params) => {
                            return h('div', [
                                h('Button', {
                                    props: {
                                        type: 'primary',
                                        size: 'small'
                                    },
                                    style: {
                                        marginRight: '5px'
                                    },
                                    on: {
                                        click: () => {
                                            this.editSchool(params.index)
                                        }
                                    }
                                }, '编辑'),
                            ]);
                        }
                    }
                ];
          
        },
        data () {
            return {
                columns: [],
                condition: '',
                data: [],
                paging: {},
                confirm: false,
                bean: 0,
                uid: 0
            }
        },
        methods: {
            load_data(paging) {
                this.$http.post(util.baseURL+"school/getSchoolList",paging,
                    {emulateJSON: true})
                    .then(
                        (response)=>{
                            if(response.data.data) {
                              this.data = response.data.data.list;
                             // console.log(this.data);
                              this.paging.total = response.data.data.total;
                            } else {
                               this.$Message.error("暂无用户列表！");
                            }
                        },
                        (error)=>{
                            console.log(error);
                        }
                );
            },
            show (index) {
                this.$Modal.info({
                    title: '用户详细信息',
                    content: `name：${this.data[index].name}<br>
                        phone：${this.data[index].phone}<br>nickname：${this.data[index].nickname}<br>
                        openid：${this.data[index].openid}<br>password：${this.data[index].password}`
                })
            },
            changepage(page) {
                this.paging.page = page;
                if (this.condition.trim() == "") {
                    this.load_data(this.paging);
                } else {
                    this.search()
                }
            },
            
            initFormatter(){
                Date.prototype.Format = function(fmt) { //
                    let o = {  
                        "M+" : this.getMonth()+1,                 //月份  
                        "d+" : this.getDate(),                    //日  
                        "h+" : this.getHours(),                   //小时  
                        "m+" : this.getMinutes(),                 //分  
                        "s+" : this.getSeconds(),                 //秒  
                        "q+" : Math.floor((this.getMonth()+3)/3), //季度  
                        "S"  : this.getMilliseconds()             //毫秒  
                    };  
                    if(/(y+)/.test(fmt))  
                        fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));  
                    for(var k in o)  
                        if(new RegExp("("+ k +")").test(fmt))  
                            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));  
                    return fmt;  
                }
            },
           
            // 订单导出
            addSchool() {
                this.$router.push("/index/addSchool");
            },
            // 更新数据
            editSchool(index) {

                var data = this.data[index];
                var query = {
                    id: data.id,
                    name: data.name,
                    status: data.status,
                    latitude: data.latitude,
                    longitude: data.longitude,
                };
                
                this.$router.push({path:"/index/editSchool",query: query});
            },
        }
    }
</script>
